package com.casic.ops.service.impl.pipeline;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.ops.dao.pipeline.PipelineNodeInfoDAO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeInfoDO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeInfoDTO;
import com.casic.ops.entity.dto.pipeline.datastructure.node.PipelineNodeConfigInfo;
import com.casic.ops.entity.request.pipeline.CommonPageRequest;
import com.casic.ops.entity.request.pipeline.PipelineNodeInfoQueryRequest;
import com.casic.ops.entity.request.pipeline.PipelineNodeInfoRequest;
import com.casic.ops.entity.vo.pipeline.response.MultiResponse;
import com.casic.ops.entity.vo.pipeline.response.PageResponse;
import com.casic.ops.entity.vo.pipeline.response.Response;
import com.casic.ops.entity.vo.pipeline.response.SingleResponse;
import com.casic.ops.service.api.pipeline.PipelineNodeInfoService;
import com.casic.ops.utils.OpsSnowFlake;
import com.casic.ops.utils.pipeline.PipelineNodeInfoConvertor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class PipelineNodeInfoServiceImpl implements PipelineNodeInfoService {

    @Resource
    private PipelineNodeInfoDAO pipelineNodeInfoDAO;

    @Resource
    private PipelineNodeInfoConvertor pipelineNodeInfoConvertor;

    @Override
    public Response add(PipelineNodeInfoRequest pipelineNodeInfoRequest) {
        PipelineNodeInfoDTO pipelineNodeInfoDTO=pipelineNodeInfoRequest.getPipelineNodeInfoDTO();
        pipelineNodeInfoDTO.init();
        PipelineNodeInfoDO pipelineNodeInfoDO= pipelineNodeInfoConvertor.toDO(pipelineNodeInfoDTO);
        pipelineNodeInfoDO.setUuid(OpsSnowFlake.getInstance().nextId());
        QueryWrapper<PipelineNodeInfoDO> qw=new QueryWrapper<>();
        qw.eq("node_uuid",pipelineNodeInfoDO.getNodeUuid());
        int count=pipelineNodeInfoDAO.selectCount(qw);
        if(count!=0){
            return Response.error(500,"该节点信息已存在");
        }
        int result=pipelineNodeInfoDAO.insert(pipelineNodeInfoDO);
        if(result==0) {
            return Response.error("新增失败");
        }else {
            return SingleResponse.of(pipelineNodeInfoDO.getId());
        }
    }

    @Override
    public Response update(PipelineNodeInfoRequest pipelineNodeInfoRequest) {
        UpdateWrapper<PipelineNodeInfoDO> updateWrapper=new UpdateWrapper<>();
        PipelineNodeInfoDTO pipelineNodeInfoDTO=pipelineNodeInfoRequest.getPipelineNodeInfoDTO();
        updateWrapper.eq("id",pipelineNodeInfoDTO.getId());
        updateWrapper.eq("node_uuid",pipelineNodeInfoDTO.getNodeUuid());
        updateWrapper.set("info",pipelineNodeInfoDTO.getInfo());
        int result=pipelineNodeInfoDAO.update(null,updateWrapper);
        if(result==0){
            return Response.error("更新失败");
        }else{
            return Response.success();
        }
    }

    @Override
    public Response delete(Long[] ids) {
        /**
         * 逻辑删除,0为已删除，1为未删除
         */
        for(Long id:ids){
            PipelineNodeInfoDO pipelineNodeInfoDO=pipelineNodeInfoDAO.selectById(id);
            if (pipelineNodeInfoDO!=null){
                pipelineNodeInfoDO.setDeleted("0");
                pipelineNodeInfoDAO.updateById(pipelineNodeInfoDO);
            }
        }
        return Response.success();
    }

    @Override
    public SingleResponse<PipelineNodeInfoDTO> getById(PipelineNodeInfoQueryRequest request) {
        PipelineNodeInfoDO pipelineNodeInfoDO=pipelineNodeInfoDAO.selectById(request.getId());
        if (Objects.isNull(pipelineNodeInfoDO)&& StringUtils.isNotBlank(request.getNodeUuid())){
            QueryWrapper<PipelineNodeInfoDO> qw=new QueryWrapper<>();
            qw.eq("node_uuid",request.getNodeUuid());
            PipelineNodeInfoDO pipelineNodeInfoDO1=pipelineNodeInfoDAO.selectOne(qw);
            return SingleResponse.of(pipelineNodeInfoConvertor.toDTO(pipelineNodeInfoDO1));
        }else{
            return SingleResponse.of(pipelineNodeInfoConvertor.toDTO(pipelineNodeInfoDO));
        }
    }

    @Override
    public MultiResponse<PipelineNodeInfoDTO> list() {
        QueryWrapper<PipelineNodeInfoDO> qw=new QueryWrapper<>();
        List<PipelineNodeInfoDO> pipelineNodeInfoDOList=pipelineNodeInfoDAO.selectList(qw);
        List<PipelineNodeInfoDTO> pipelineNodeInfoDTOList=pipelineNodeInfoConvertor.toDTOList(pipelineNodeInfoDOList);
        return MultiResponse.of(pipelineNodeInfoDTOList);
    }

    @Override
    public PageResponse<PipelineNodeInfoDTO> page(CommonPageRequest request) {
        QueryWrapper<PipelineNodeInfoDO> qw=new QueryWrapper();
        Page<PipelineNodeInfoDO> page=new Page(request.getPageIndex(),request.getPageSize());
        IPage<PipelineNodeInfoDO> iPage=pipelineNodeInfoDAO.selectPage(page,qw);
        /**
         * 查询总数
         */
        Integer count=pipelineNodeInfoDAO.selectCount(qw);
        PageResponse<PipelineNodeInfoDTO> response=new PageResponse<>();
        response.setTotalCount(count);
        List<PipelineNodeInfoDTO> pipelineNodeInfoDTOList=pipelineNodeInfoConvertor.toDTOList(iPage.getRecords());
        response.setData(pipelineNodeInfoDTOList);
        return response;
    }

    @Override
    public PipelineNodeConfigInfo selectByNodeUuid(String nodeUuid) {
        QueryWrapper<PipelineNodeInfoDO> qw=new QueryWrapper<>();
        qw.eq("node_uuid",nodeUuid);
        PipelineNodeInfoDO pipelineNodeInfoDO=pipelineNodeInfoDAO.selectOne(qw);
        if(pipelineNodeInfoDO==null){
            return null;
        }
        PipelineNodeConfigInfo pipelineNodeConfigInfo =new PipelineNodeConfigInfo();
        pipelineNodeConfigInfo.setNodeUuid(pipelineNodeInfoDO.getNodeUuid());
        pipelineNodeConfigInfo.setInfo(pipelineNodeInfoDO.getInfo());
        pipelineNodeConfigInfo.setUuid(pipelineNodeInfoDO.getUuid());
        pipelineNodeConfigInfo.setDeleted(pipelineNodeInfoDO.getDeleted());
        return pipelineNodeConfigInfo;
    }
}
